import request from '@/utils/request'

// 查询信贷信息分页列表
export function pageQueryCreditInfo(query) {
  return request({
    url: '/yjnb/creditInfo/page',
    method: 'get',
    params: query
  })
}

// 根据ID查询信贷信息详细
export function getCreditInfo(id) {
  return request({
    url: '/yjnb/creditInfo/getByid/' + id,
    method: 'get'
  })
}

// 新增信贷信息
export function addCreditInfo(data) {
  return request({
    url: '/yjnb/creditInfo/add',
    method: 'post',
    data: data
  })
}

// 修改信贷信息
export function updateCreditInfo(data) {
  return request({
    url: '/yjnb/creditInfo/update',
    method: 'put',
    data: data
  })
}

// 批量删除信贷信息
export function delCreditInfo(ids) {
  // 确保传入的是数组
  const idList = Array.isArray(ids) ? ids : [ids];

  // 过滤掉空值
  const validIds = idList.filter(id => id !== null && id !== undefined);

  if (validIds.length === 0) {
    return Promise.reject(new Error('请选择要删除的信贷信息'));
  }

  // 拼接URL参数：ids=1&ids=2&ids=3
  const queryString = validIds.map(id => `ids=${id}`).join('&');

  return request({
    url: `/yjnb/creditInfo/deletes?${queryString}`,
    method: 'delete'
  })
}

// 批量修改信贷信息状态
export function updateCreditInfoStatus(ids, status) {
  // 确保传入的是数组
  const idList = Array.isArray(ids) ? ids : [ids];

  // 过滤掉空值
  const validIds = idList.filter(id => id !== null && id !== undefined);

  if (validIds.length === 0) {
    return Promise.reject(new Error('请选择要操作的信贷信息'));
  }

  // 拼接URL参数：ids=1&ids=2&ids=3&status=1
  const queryString = validIds.map(id => `ids=${id}`).join('&') + `&status=${status}`;

  return request({
    url: `/yjnb/creditInfo/updateStatus?${queryString}`,
    method: 'put'
  })
}

// 批量修改信贷信息推荐状态
export function updateCreditInfoRecommend(ids, isRecommend) {
  // 确保传入的是数组
  const idList = Array.isArray(ids) ? ids : [ids];

  // 过滤掉空值
  const validIds = idList.filter(id => id !== null && id !== undefined);

  if (validIds.length === 0) {
    return Promise.reject(new Error('请选择要操作的信贷信息'));
  }

  // 拼接URL参数：ids=1&ids=2&ids=3&isRecommend=1
  const queryString = validIds.map(id => `ids=${id}`).join('&') + `&isRecommend=${isRecommend}`;

  return request({
    url: `/yjnb/creditInfo/updateRecommend?${queryString}`,
    method: 'put'
  })
}

// 导出信贷信息数据
export function exportCreditInfo() {
  return request({
    url: '/common/excel/creditInfo/export',
    method: 'get',
    responseType: 'blob'
  })
}

